package com.javakc.pms.instruction.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.commonutils.api.R;
import com.javakc.pms.instruction.entity.Instruction;
import com.javakc.pms.instruction.service.InstructionService;
import com.javakc.servicebase.config.InstructionQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="调度指令库管理")
@RestController
@RequestMapping("/pms/instruction")

@CrossOrigin
public class InstructionController {

    @Autowired
    private InstructionService instructionService;

    @GetMapping
    public R findAll(){
        List<Instruction> list = instructionService.list();
        return R.OK().data("items",list);
    }

    @ApiOperation(value = "根据条件进行分页查询 - 调度指令库")
    @PostMapping("{pageNo}/{pageSize}")
    public R findPageIntruction(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Long pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Long pageSize,
            @ApiParam(name = "instructionQuery", value = "查询条件封装对象", required = false)
            @RequestBody(required = false) InstructionQuery instructionQuery) {
        Page<Instruction> instructionPage  = new Page<>(pageNo, pageSize);
        instructionService.findPageInstruction(instructionPage,instructionQuery);
        long total = instructionPage.getTotal();
        List<Instruction> instructionList = instructionPage.getRecords();
        return R.OK().data("total", total).data("items", instructionList);
    }

    @ApiOperation(value="新增调度指令库")
    @PostMapping("saveIntruction")
    public R saveIntruction(@RequestBody Instruction instruction){
        boolean flag   = instructionService.save(instruction);
        if(flag)
        {
            return R.OK();
        }else{
            return R.ERROR();
        }
    }

    @ApiOperation(value = "根据ID获取调度指令库")
    @GetMapping("getInstructionByid/{instructionId}")
    public R getIntructionById(@PathVariable("instructionId") int instructionId){
        Instruction instruction = instructionService.getById(instructionId);
        return R.OK().data("instruction",instruction);
    }

    @ApiOperation(value = "修改调度指令库")
    @PutMapping("updateInstruction")
    public R updateIntrucation(@RequestBody Instruction instruction){
        boolean flag = instructionService.updateById(instruction);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value="根据ID删除调度指令库")
    @DeleteMapping("deleteInstructionId/{instructionId}")
    public R deleteIntructionById(@PathVariable("instructionId")String instructionId){
        boolean flag = instructionService.removeById(instructionId);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }
}
